#pragma once
#include "platform.hpp"
#include "thread/atomiccounter.hpp"
using namespace JHCPP::thread;

#if defined(JH_OS_WINDOWS)
#include <Windows.h>
#elif defined(JH_OS_LINUX)

#endif

namespace JHCPP
{
	namespace filesys
	{
	#if defined(JH_OS_WINDOWS)
		class CPipeImpl: public CRefCountedObject
		{
		public:
			typedef HANDLE Handle;

			CPipeImpl();
			~CPipeImpl();
			int writeBytes(const void* buffer, int length);
			int readBytes(void* buffer, int length);
			Handle readHandle() const;
			Handle writeHandle() const;
			void closeRead();
			void closeWrite();

		private:
			HANDLE m_readHandle;
			HANDLE m_writeHandle;
		};
	#elif defined(JH_OS_LINUX)
		class CPipeImpl: public CRefCountedObject
		{
		public:
			typedef int Handle;

			CPipeImpl();
			~CPipeImpl();
			int writeBytes(const void* buffer, int length);
			int readBytes(void* buffer, int length);
			Handle readHandle() const;
			Handle writeHandle() const;
			void closeRead();
			void closeWrite();

		private:
			int m_readfd;
			int m_writefd;
		};
	#endif

		class CPipe
		{
		public:
			typedef CPipeImpl::Handle Handle; 

			enum CloseMode
			{
				CLOSE_READ  = 0x01, 
				CLOSE_WRITE = 0x02,
				CLOSE_BOTH  = 0x03 
			};

			CPipe();
			CPipe(const CPipe& pipe);
			~CPipe();

			CPipe& operator = (const CPipe& pipe);

			int writeBytes(const void* buffer, int length);
			int readBytes(void* buffer, int length);

			Handle readHandle() const;
			Handle writeHandle() const;

			void close(CloseMode mode = CLOSE_BOTH);

		private:
			CPipeImpl* m_pImpl;
		};

	}//end of namespace filesys
}//end of namespace JHCPP
